/*
 * Copyright © OpenAtom Foundation.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *      http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package io.iec.edp.caf.msu.server.storage.database;

import io.iec.edp.caf.msu.server.storage.MsuStorage;
import io.iec.edp.caf.msu.server.storage.database.domain.entity.GspMsuRegisterInfo;
import io.iec.edp.caf.msu.server.storage.database.domain.manager.GspMsuRegisterManager;
import lombok.var;

import java.util.ArrayList;
import java.util.List;

public class MsuStorageDatabaseImpl implements MsuStorage {

    private GspMsuRegisterManager manager;

    public MsuStorageDatabaseImpl(GspMsuRegisterManager manager) {
        this.manager = manager;
    }

    @Override
    public Boolean save(List<GspMsuRegisterInfo> infoList) {
        this.manager.saveMsuInfo(infoList);
        return true;
    }

    @Override
    public List<String> getMsuServiceList(String appName, String serviceUnitName) {
        var infoList= this.manager.getMsuRegisterInfo(appName, serviceUnitName);
        var nameList = new ArrayList<String>();
        if (infoList != null && infoList.size() > 0) {
            for (var info : infoList) {
                nameList.add(info.getServiceName());
            }
        }
        return nameList;
    }

    @Override
    public List<String> getAllServiceName() {
        var infoList=this.manager.getAllInfo();
        var nameList=new ArrayList<String>();
        for (var info:infoList){
            if(!nameList.contains(info.getServiceName()))
                nameList.add(info.getServiceName());
        }
        return nameList;
    }

    @Override
    public Boolean deleteMsuInfo(List<GspMsuRegisterInfo> infoList) {
        for (var info : infoList) {
            this.manager.deleteMsuInfo(info.getId());
        }
        return true;
    }

    @Override
    public void deleteService(List<String> serviceList) {
        this.manager.deleteService(serviceList);
    }

    @Override
    public void deleteBySu(List<String> sus) {
        this.manager.deleteBySu(sus);
    }

    @Override
    public boolean deleteByAppAndSu(String appName, List<String> sus) {
        var check = false;
        this.manager.deleteByAppAndSu(appName,sus);
        check =true;
        return check;
    }

    @Override
    public List<GspMsuRegisterInfo> getEnabledServiceUnitInfo() {
        return this.manager.getEnabledServiceUnitInfo();
    }


}
